package News.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://localhost:3306/newspublish";
	private static final String UNAME = "root";
	private static final String UPWD = "wangchao";
	
	private static Connection connection = null;
	
	public static Connection getConnection(){
			try {
				Class.forName(DRIVER);
				if(connection ==null || connection.isClosed()){
					connection = DriverManager.getConnection(URL,UNAME,UPWD);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return connection;
	}
	public static void closeAll(ResultSet rs,PreparedStatement pstmt,Connection connection){
		try {
			if(rs != null){
				rs.close();
			}
			if(pstmt != null){
				pstmt.close();
			}
			if(connection != null){
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}